<?php

namespace App\Admin\Controllers\exam;

use App\Admin\Controllers\exam\actions\JumpEdit;
use App\Admin\Controllers\exam\actions\JumpUserExam;
use App\Common\statement\CommonStatement;
use App\Models\exams\Exam;
use App\Models\exams\ExamQuestion;
use App\Models\exams\Question;
use App\Models\exams\UserExam;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ExamController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '试卷';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Exam());

        $grid->model()->latest();
        $grid->disableExport();
        $grid->column('id', __('ID'));
        $grid->column('title', __('Title'));
        $grid->column('intro', __('Intro'));
        $grid->column('start_time', __('Start Time'));
        $grid->column('end_time', __('End Time'));
        $grid->column('status',__('Status'))->radio(CommonStatement::examstatus);
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));
        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->add(new JumpEdit());
            if ($actions->row->status == 2) {
                $actions->add(new JumpUserExam());
            }
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Exam::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('cate_id', __('Cate'));
        $show->field('type', __('Type'));
        $show->field('title', __('Title'));
        $show->field('content', __('Content'))->editor();
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Exam());
        $form->column(1/3, function ($form) {
            $form->text('title',__('Title'));
            $form->image('cover_img',__('Cover'));
            $form->text('intro',__('简介'));
            $form->date('start_time',__('开始时间'));
            $form->date('end_time',__('结束时间'));
            $form->number('minute',__('考试时间(分钟)'));
            $form->radio('status',__('Status'))->options(CommonStatement::examstatus);
        });

        $form->column(2/3, function ($form) {
            $form->table('group_exam',__('组卷(已开始答题后请勿重新组卷)'), function ($table) {
                $table->select('cate',__('分类'))->options(CommonStatement::question_cate)->required();
                $table->text('number',__('题数'))->default(1);
            })->required();
        });

        $form->saving(function (Form $form) {

        });

        $form->saved(function (Form $form){
            $change = $form->model()->getChanges();
            if (isset($change['group_exam'])){
                $question = [];
                ExamQuestion::where('exam_id',$form->model()->id)->update(['status'=>2]);
                foreach ($form->model()->group_exam as $item){
                    $question =  array_merge($question,Question::where('cate',$item['cate']) ->inRandomOrder()
                        ->take($item['number'])->select(['id','type'])->get()->toArray());
                }
                foreach ($question as $value){
                    $param = [
                        'exam_id' => $form->model()->id,
                        'question_id' => $value['id'],
                        'status' => 1
                    ];
                    ExamQuestion::firstOrCreate($param);
                }
            }
        });

        return $form;
    }
}
